package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.example.entity.User;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:0xOO
 * @Date: 2018/9/26 0026
 * @Time: 14:42
 */

@Controller
@RequestMapping("/testBoot")
@Api(value = "测试用户信息")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;

    @GetMapping("getUser/{id}")
    @ApiOperation(value = "获取用户信息", notes = "通过用户ID获取用户信息")
    @ResponseBody
    public User GetUser(@PathVariable int id) {
            userService.Sel(id);
      return userService.Sel(id);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/queryByPage")
    @ResponseBody
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数",  dataType = "Integer")
    })
    private IPage<User> queryByPage(Integer page,Integer size) {
        IPage<User> userIPage = userMapper.selectPage(new Page<>(page, size), null);
        return userIPage;
    }

    /***
     * 调用函数
     * @return
     */
    @GetMapping("getVersion")
    @ApiOperation(value = "查询函数", notes = "查询函数")
    @ResponseBody
    public String getABS() {
        return userService.getVersion();
    }

    /**
     * 调用存储过程
     * @return
     */
    @GetMapping("testProcedure")
    @ApiOperation(value = "查询存储过程", notes = "查询存储过程")
    @ResponseBody
    public Integer testProcedure(Integer adc) {
        HashMap params = new HashMap();
        params.put("test1", adc);
        params.put("test2", 0);
        userMapper.testProcedure(params);
        Integer test2 = (Integer) params.get("test2");
        return test2;
    }
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加用户信息", notes = "添加用户的信息")
    private String add(User user) throws Exception{
        userService.add(user);
        return "添加成功";
    }
    @PutMapping("/update")
    @ResponseBody
    @ApiOperation(value = "修改用户信息", notes = "修改用户的信息")
    private String update(User user) {
        Integer id = user.getId();
        if (id ==null){
            return "该信息不存在" ;
        }
        userService.update(user);
        return "修改成功";
    }
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    @ApiOperation(value = "删除用户信息", notes = "删除用户的信息")
    private String delete(@PathVariable int id) {
        userService.delete(id);
        return "删除成功";
    }


}
